const {
  getNbThings,
  createNbThing,
  createNbThings,
} = require("@/models/v1/nb-thing-model");
const { moveFile } = require("@/utils");
const _ = require("lodash");

exports.page = async (req, res, next) => {
  try {
    const result = await getNbThings(req.query);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

exports.create = async (req, res, next) => {
  try {
    const bodyParams = _.omitBy(
      req.body,
      (value) => value === null || value === undefined || value === ""
    ); // 去掉空值
    const result = await createNbThing(bodyParams);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

exports.createBatch = async (req, res, next) => {
  try {
    var rows = req.body.rows;
    if (!rows || !_.isArray(rows) || rows.length < 1)
      throw new Error("请传入时间列表");
    _(rows).forEach((item, index) => {
      if (!item.title) {
        throw new Error(`请输入第${index + 1}条的事件名称`);
      }
      if (!item.start_at) {
        throw new Error(`请选择${item.title}的开始时间`);
      }
      // 处理文件 [_temp/uploads/c4518cd8b77335b06a1e68920d09caee.jpg]
      if (files && files.length > 0) {
        files.forEach((file) => {
          // 移动文件
          // moveFile();
        });
      }
      if (!item.end_at) delete item.end_at;
    });

    // 插入数据库
    const result = await createNbThings(rows);

    // 移动文件

    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};
